package com.woniu.controller;

import com.woniu.entity.PageResult;
import com.woniu.entity.Result;
import com.woniu.pojo.Medicine;
import com.woniu.service.IMedicineService;
import com.woniu.vo.DrugQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 药品表 前端控制器
 * </p>
 *
 * @author 刘岩
 * @since 2022-10-10
 */
@RestController
@RequestMapping("/medicine")
@Api(tags = "药品管理")
public class MedicineController extends BaseController{

    @Autowired
    private IMedicineService drugService;

    @ApiOperation("查询所有")
    @GetMapping("queryAll")
    public Result queryAll(){
        List<Medicine> medicineList = drugService.list();
        return toDataResult(medicineList);
    }

    @ApiOperation("分页模糊搜索药品")
    @PostMapping("/search/{currentPage}/{pageSize}")
    public Result search(
            @PathVariable Integer currentPage,
            @PathVariable Integer pageSize,
            @RequestBody DrugQueryVo drugQueryVo){
        PageResult pageResult = drugService.search(currentPage, pageSize, drugQueryVo);
        return toDataResult(pageResult);
    }

    @ApiOperation("根据ID查询药品")
    @GetMapping("/id/{id}")
    public Result queryById(@PathVariable Long id) {
        Medicine medicine = drugService.getById(id);
        return toDataResult(medicine);
    }

    @ApiOperation("根据名字查询药品")
    @GetMapping("/title/{title}")
    public Result queryByTitle(@PathVariable String title){
        List<Medicine> medicineList = drugService.queryByTitle(title);
        return toDataResult(medicineList);
    }

    @ApiOperation("增加药品")
    @PostMapping
    public Result add(@RequestBody Medicine medicine){
        boolean flag = drugService.save(medicine);
        return toResult(flag);
    }
    @ApiOperation("编辑药品")
    @PutMapping
    public Result edit(@RequestBody Medicine medicine) {
        //刷新修改时间
        medicine.setUpdateTime(new Date());
        //需要返回一个课程ID
        boolean flag = drugService.updateById(medicine);
        return toResult(flag);
    }

    @ApiOperation("根据ID删除药品")
    @DeleteMapping("/{id}")
    public Result remove(@PathVariable Long id) {
        boolean flag = drugService.removeById(id);
        return toResult(flag);
    }

    @ApiOperation("批量删除药品")
    @PostMapping("/batch")
    public Result removeBatch(@RequestBody List<Long> ids) {
        boolean flag = drugService.removeBatchByIds(ids);
        return toResult(flag);
    }

    @ApiOperation("修改药品状态")
    @PutMapping("/sta/{medicineId}")
    public Result setStatus(@PathVariable Long medicineId){
        boolean flag = drugService.saveStatus(medicineId);
        return toResult(flag);
    }

    @ApiOperation("查询药品排序的最大值")
    @GetMapping("/sort")
    public Result queryMaxSortByCourseId(){
        Integer maxSort = drugService.queryMaxSortByCourseId();
        return toDataResult(maxSort);
    }

}
